#include "ClientSocket.h"

ClientSocket :: ClientSocket ( char* ipServidor,int port ) : Socket ( port ) {
	strcpy ( this->ipServidor,ipServidor );
}

ClientSocket :: ~ClientSocket () {
}

void ClientSocket :: abrirConexion () {

	struct hostent *server = gethostbyname ( this->ipServidor );
	if ( server == NULL )
		throw "No se puede localizar el host";

    bcopy ( (char *)server->h_addr,(char *)&(this->serv_addr.sin_addr.s_addr),server->h_length );

    int connectOk = connect ( this->fdSocket,(const struct sockaddr *)&(this->serv_addr),sizeof(this->serv_addr) );
    if ( connectOk < 0 )
    	throw "Error en connect";
}

int ClientSocket :: enviar ( char* buffer ) {
	int cantBytes = write ( this->fdSocket,buffer,strlen(buffer) );
	return cantBytes;
}

int ClientSocket :: recibir ( char* buffer,int buffSize ) {
	int cantBytes = read ( this->fdSocket,buffer,buffSize );
	return cantBytes;
}

void ClientSocket :: cerrarConexion () {
	close ( this->fdSocket );
}
